# -*- coding: utf-8 -*-
import pygame
from GameBase import GameBase
from Snake import Snake
from Food import Food
from Setting import *

class SnakeGame(GameBase):

    def __init__(self):
        super(SnakeGame, self).__init__(
            game_name=GAME_NAME,
            screen_size=((WIDTH + 2 * 30, HEIGHT + 2 * 30)),
            fps=FPS,
            font_name=FONT_NAME,
            font_size=FONT_SIZE,
            display_mode=pygame.HWSURFACE | pygame.DOUBLEBUF,
            icon=ICON)

        self.show_start_screen()
        self.game_info = pygame.Surface((WIDTH, 30))
        self.game_zone = pygame.Surface((WIDTH + 1, HEIGHT + 1))
        self.snake = Snake(self)
        self.food = Food(self)

        self.add_key_bind(pygame.K_UP, self.snake.turn, direction=UP)
        self.add_key_bind(pygame.K_DOWN, self.snake.turn, direction=DOWN)
        self.add_key_bind(pygame.K_LEFT, self.snake.turn, direction=LEFT)
        self.add_key_bind(pygame.K_RIGHT, self.snake.turn, direction=RIGHT)
        self.add_key_bind(pygame.K_r, self.snake.respawn)
        self.add_display_action(self.draw)
        self.add_display_action(self.draw_background)

    def show_start_screen(self):
        game_name = pygame.font.SysFont(self.font_name, 80).render(GAME_NAME, True, (255, 255, 255))
        tip = pygame.font.SysFont(self.font_name, 20).render('Press any key to play', True, (255, 255, 255))
        while True:
            if pygame.event.get(pygame.KEYUP):
                pygame.event.get()
                self.current_tick = pygame.time.get_ticks()
                break
            self.draw_image_center(self.screen, game_name, self.screen.get_rect())
            self.draw_image_rightbottom(self.screen, tip, self.screen.get_rect().inflate(-20, -20))
            pygame.display.flip()
            self.clock.tick(self.fps)

    def draw(self):
        self.background.fill((0, 0, 0))

        # 更新信息区
        self.game_info.fill((0, 0, 0))
        if not self.running:
            status = u'Pause'
        else:
            status = u'Run' if self.snake.alive else u'Dead'
        info = '  '.join([
            '[Score: %u]' % self.snake.score,
            '[Speed: %u]' % self.snake.speed,
            '[Status: %s]' % status,
            '[Fps: %.1f]' % self.clock.get_fps()])
        out = self.font.render(info, True, (255, 255, 255))
        self.game_info.blit(out, (0, 5))

        # 更新游戏区域
        self.game_zone.fill((0, 0, 0))
        for i in range(0, WIDTH + 1, CELL):
            pygame.draw.line(self.game_zone, (33, 33, 33), (i, 0), (i, HEIGHT))
        for i in range(0, HEIGHT + 1, CELL):
            pygame.draw.line(self.game_zone, (33, 33, 33), (0, i), (WIDTH, i))

        # 绘制蛇和食物
        self.food.draw()
        self.snake.draw()

        # 贴图
        self.background.blit(self.game_info, (30, 0))
        self.background.blit(self.game_zone, (30, 30))

    def draw_image_center(self, dst, src, rect):
        w, h = src.get_size()
        centerx, centery = rect.center
        x, y = centerx - w / 2, centery - h / 2
        dst.blit(src, (x, y))

    def draw_image_rightbottom(self, dst, src, rect):
        w, h = src.get_size()
        centerx, centery = rect.center
        x, y = rect.right - w, rect.bottom - h
        dst.blit(src, (x, y))

if __name__ == '__main__':
    game = SnakeGame()
    game.run()
